import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/Login.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("收到用户名：" + username);
        System.out.println("收到密码：" + password);

        if ("root".equals(username) && "123123".equals(password)) {
            req.getSession().setAttribute("username",username);
            resp.sendRedirect(req.getContextPath() + "/stu");

        }else {
            req.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("Login.jsp").forward(req,resp);
        }
    }
}
